﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using UFSoft.UBF.Business;
using UFSoft.UBF.PL;
using UFSoft.UBF.Service.Base;

namespace UFIDA.U9.Cust.Pub.WS.ProxyService.Json
{
    /// <summary>
    ///     代理类解析器
    /// </summary>
    public class ProxyBaseContractResolver : BaseContractResolver
    {
        public ProxyBaseContractResolver(bool useDataContractTransData) : base(useDataContractTransData)
        {
        }

        protected override List<MemberInfo> GetSerializableMembers(Type objectType)
        {
            List<MemberInfo> oldMemberInfos = base.GetSerializableMembers(objectType);
            List<MemberInfo> newMemberInfos = new List<MemberInfo>();
            foreach (MemberInfo memberInfo in oldMemberInfos)
            {
                //DataTransObjectBase
                if (memberInfo.DeclaringType == typeof(DataTransObjectBase))
                {
                    continue;
                }

                //contextdto
                if (memberInfo.DeclaringType != null && memberInfo.DeclaringType.IsSubclassOf(typeof(ProxyBase)))
                {
                    string memberName = memberInfo.Name.ToLower(CultureInfo.CurrentCulture);
                    if (memberName == "contextdto" || memberName == "targetorgcode" || memberName == "targetorgname" ||
                        memberName.EndsWith("_skey")) continue;
                }

                //BusinessEntity.EntityKey
                if (memberInfo.DeclaringType != null &&
                    (memberInfo.DeclaringType == typeof(ObjectKeyX) ||
                     memberInfo.DeclaringType.IsSubclassOf(typeof(ObjectKeyX))))
                {
                    string memberName = memberInfo.Name.ToLower(CultureInfo.CurrentCulture);
                    //if (memberName == "entitytype" || memberName == "serializablekeys") continue;
                    //去掉了entitytype过滤，因为BusinessEntity.EntityKey 需要指定entitytype
                    if (memberName == "serializablekeys") continue;
                }

                bool isDTOType = false;
                switch (memberInfo.MemberType)
                {
                    case MemberTypes.Field:
                        FieldInfo fieldInfo = (FieldInfo) memberInfo;
                        if (fieldInfo.DeclaringType != null &&
                            fieldInfo.FieldType.IsSubclassOf(typeof(DataTransObjectBase)))
                        {
                            isDTOType = true;
                        }

                        break;
                    case MemberTypes.Property:
                        PropertyInfo propertyInfo = (PropertyInfo) memberInfo;
                        if (propertyInfo.DeclaringType != null &&
                            propertyInfo.DeclaringType.IsSubclassOf(typeof(DataTransObjectBase)))
                        {
                            isDTOType = true;
                        }

                        break;
                }

                if (isDTOType)
                {
                    string memberName = memberInfo.Name.ToLower(CultureInfo.CurrentCulture);
                    if (memberName == "sysentitytype" || memberName == "combinename" ||
                        memberName.StartsWith("multi_") ||
                        memberName.EndsWith("_skey")) continue;
                }

                newMemberInfos.Add(memberInfo);
            }

            return newMemberInfos;
        }
    }
}